//
//  AdCell.swift
//  clreader
//
//  Created by colin on 2019/3/6.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

extension HomeViewController {
    
    class AdCell: UITableViewCell {
        
        static let height:CGFloat = 80.0 * ((UIScreen.main.bounds.width - 25.0) / 350.0)
        static let reuseId = "AdCell"
        
        private lazy var imageViewPhoto = UIImageView()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            
            self.contentView.addSubview(self.imageViewPhoto)
            self.imageViewPhoto.snp.makeConstraints({ (make) in
                let width = UIScreen.main.bounds.width - 25.0
                make.size.equalTo(CGSize(width: 350.0 * (width / 350.0), height: 80.0 * (width / 350.0)))
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
            })
            
        }
        
        func refresh(datas:[JSONObject]) {
            if !datas.isEmpty {
                if let advImage = datas[0]["adv_img"] as? String, !advImage.isEmpty,
                    let baseUrl = URL(string: Router.baseUrl) {
                    let url = baseUrl.appendingPathComponent(advImage)
                    self.imageViewPhoto.kf.setImage(with: url)
                } else {
                    self.imageViewPhoto.image = nil
                }
            } else {
                self.imageViewPhoto.image = nil
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}
